package com.bcbim.model_server.component.service;

import com.bcbim.model_server.component.mapper.ComponentMapper;
import com.bcbim.model_server.component.mapper.ComponentMaterialMapper;
import com.bcbim.model_server.component.mapper.ComponentParamMapper;
import com.bcbim.model_server.component.mapper.ComponentTreeMapper;
import com.bcbim.model_server_interface.component.bo.*;
import com.bcbim.model_server_interface.component.dto.CategoryComponentDTO;
import com.bcbim.model_server_interface.component.dto.ComponentMaterialDTO;
import com.bcbim.model_server_interface.component.service.ComponentService;
import com.bcbim.model_server_interface.component.vo.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 构件接口
 * @Author: ye
 * @Date: 2022/4/12
 * @Version: V1.0
 **/
@Service(timeout = 60 * 1000, group = "component", version = "1.0.0")
public class ComponentServiceImpl implements ComponentService {
    @Autowired
    private ComponentMapper componentMapper;
    @Autowired
    private ComponentMaterialMapper componentMaterialMapper;
    @Autowired
    private ComponentTreeMapper componentTreeMapper;
    @Autowired
    private ComponentParamMapper componentParamMapper;

    /**
     * @Description: 查询构件id
     * @Param: ComponentMaterialDTO
     * @Return: ComponentMaterialBO
     * @Author: ye
     * @Date: 2022/4/12
     */
    @Override
    public ComponentMaterialBO getComponentId(ComponentMaterialDTO query) {
        return componentMapper.getComponentId(query);
    }

    /**
     * @Description: 根据构件id查询材质索引
     * @Param: componentId
     * @Return: List<MaterialIndexVO>
     * @Author: ye
     * @Date: 2022/4/12
     */
    @Override
    public List<MaterialIndexVO> getComponentMashById(String componentId) {
        return componentMaterialMapper.getComponentMashById(componentId);
    }

    /**
     * @Description: 查询文件的组件树
     * @Param: fileId
     * @Return: List<ComponentTreeBO>
     * @Author: ye
     * @Date: 2022/3/30
     */
    @Override
    public List<ComponentTreeBO> getComponentTree(String fileId) {
        return componentTreeMapper.getComponentTree(fileId);
    }

    /**
     * @Description: 查询构件树的组件
     * @Param: fileId
     * @Return: List<ComponentTreeBO>
     * @Author: ye
     * @Date: 2022/3/30
     */
    @Override
    public List<ComponentTreeBO> getComponentTreeComponent(String fileId) {
        return componentMapper.getComponentTreeComponent(fileId);
    }

    /**
     * @Description: 根据id 查询构件信息
     * @Param: componentId
     * @Return: ComponentMaterialBO
     * @Author: ye
     * @Date: 2022/4/12
     */
    @Override
    public ComponentMaterialBO getComponentInfo(String componentId) {
        return componentMapper.getComponentById(componentId);
    }

    /**
     * @Description: 根据类别查询构件
     * @Param: CategoryComponentDTO
     * @Return: List<ComponentInfoVO>
     * @Author: ye
     * @Date: 2022/4/29
     */
    @Override
    public List<ComponentInfoVO> getComponentByCategory(CategoryComponentDTO query) {
        List<ComponentInfoBO> list = componentMapper.getComponentByCategory(query);
        List<ComponentParamBO> param = componentParamMapper.getComponentParamByCategory(query);
        Map<String, Map<String, String>> paramMap = new HashMap<>();

        if (param != null && param.size() > 0) {
            for (ComponentParamBO item : param) {
                Map<String, String> stringStringMap;
                if (paramMap.containsKey(item.getComponentId())) {
                    stringStringMap = paramMap.get(item.getComponentId());
                } else {
                    stringStringMap = new HashMap<>();
                    paramMap.put(item.getComponentId(), stringStringMap);
                }
                stringStringMap.put(item.getKey(), item.getValue());
            }
        }
        List<ComponentInfoVO> result = new ArrayList<>();

        for (ComponentInfoBO componentInfoBO : list) {
            ComponentInfoVO vo = new ComponentInfoVO(componentInfoBO);
            if (paramMap.containsKey(vo.getId())) {
                vo.setParam(paramMap.get(vo.getId()));
            }
            result.add(vo);
        }
        return result;
    }

    /**
     * @Description: 查询所有的构件中点
     * @Param: String[]
     * @Return: List<ComponentCenterVO>
     * @Author: ye
     * @Date: 2022/5/13
     */
    @Override
    public List<ComponentCenterVO> getComponentCenter(String[] arrFileId) {
        List<ComponentCenterBO> list = componentMapper.getComponentCenter(arrFileId);
        List<ComponentCenterVO> voList = new ArrayList<>();
        list.forEach((item) ->{
            voList.add(new ComponentCenterVO(item));
        });
        return voList;
    }

    /**
     * @Description: 批量查询构件信息
     * @Param: componentIds
     * @Return: List<ComponentVO>
     * @Author: ye
     * @Date: 2022/6/2
     */
    @Override
    public List<ComponentVO> getArrComponentsById(String[] componentIds) {
        List<ComponentMaterialBO> list = componentMapper.getArrComponentsById(componentIds);
        List<ComponentVO> result = new ArrayList<>();
        for (ComponentMaterialBO componentMaterialBO : list) {
            ComponentVO componentVO = new ComponentVO(componentMaterialBO);
            result.add(componentVO);
        }
        return result;
    }

    /**
     * @Description: 批量查询构件mesh
     * @Param: componentIds
     * @Return: List<MaterialIndexVO>
     * @Author: ye
     * @Date: 2022/6/2
     */
    @Override
    public List<MaterialIndexVO> getArrComponentMashById(String[] componentIds) {
        return componentMaterialMapper.getArrComponentMashById(componentIds);
    }

    /**
     * @Description: 获取楼层所有构件id
     * @Param: floorId
     * @Return: List<String>
     * @Author: ye
     * @Date: 2022/6/8
     */
    @Override
    public List<String> getFloorComponentId(String floorId) {
        return componentMapper.getFloorComponentId(floorId);
    }


}
